import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getPosIDs, getUserDepID, getUserID, requestFunComponFiex, getTenantID, requestFunHtml } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { useRequest } from 'umi';
import { getManageTabSwitch } from '../mconfig';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang'; // 本地mock

/**
 * 获取验收入库详情
 * @param getDetailCallback
 * @returns
 */
const getDetailService = (getDetailCallback: (res: any) => void) => {
  const getDetail = useRequest(
    (id: any) => {
      let personUrl = `${baseUrl}/assetStorage/details/${getUserID()}`;
      const body = {
        id: id,
      };

      return requestFunComponFiex('POST', personUrl, body);
    },
    {
      manual: true,
      onSuccess: (res) => {
        getDetailCallback(res);
      },
    },
  );
  return {
    getDetailRun: getDetail?.run,
    getDetailLoading: getDetail?.loading,
  };
};

/**
 * 获取仓库树列表
 * @param getStorageCallback
 * @returns
 */
const getStorageTreeService = (getStorageCallback: (res: any) => void, curRes: any) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = getManageTabSwitch(curRes);

  const getStorage = useRequest(
    () => {
      let personUrl = `${baseUrl}/assetStore/treeList/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', personUrl, { ...queryTypeObj });
    },
    {
      manual: true,
      onSuccess: (res) => {
        getStorageCallback(res);
      },
    },
  );
  return {
    getStorageRun: getStorage?.run,
    getStorageLoading: getStorage?.loading,
  };
};

/**
 * 添加入库提交流程
 * @param addStorageCallback
 * @returns
 */
const addStorageService = (addStorageCallback: (res: any) => void) => {
  let tempFlag = false;
  const addStorage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetStorage/add`;
      let bodyParams = {
        ...params,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        state: `${getTenantID()}${ILoadMapTypeAll.ENTER_STORE_STATUS}1000`, // 起草状态
        businessType: `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}1020`, // 审批流需要用到的
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res) => {
        addStorageCallback(res);
      },
    },
  );

  return {
    addStorageRun: addStorage?.run,
    addStorageLoading: addStorage?.loading,
  };
};

/**
 * 编辑入库提交流程
 * @param editStorageCallback
 * @returns
 */
const editStorageService = (editStorageCallback: (res: any) => void) => {
  const editStorage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetStorage/edit`;
      let bodyParams = {
        ...params,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res) => {
        editStorageCallback(res);
      },
    },
  );

  return {
    editStorageRun: editStorage?.run,
    editStorageLoading: editStorage?.loading,
  };
};

/**
 * 登记接口
 * @param enrolStorageCallback
 * @returns
 */
const enrolStorageService = (enrolStorageCallback: (res: any) => void) => {
  const enrolStorage = useRequest(
    (id: any) => {
      let personUrl = `${baseUrl}/assetStorage/enroll`;
      let bodyParams = {
        id,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        enrolStorageCallback(res);
      },
    },
  );

  return {
    enrolStorageRun: enrolStorage?.run,
    enrolStorageLoading: enrolStorage?.loading,
  };
};

/** 资产调拨审批通过后调用 */
const approvedEnterSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/assetStorage/approved`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    approvedEnterRun: request?.run,
    approvedEnterLoading: request?.loading,
  };
};

/**
 * 添加流程条件
 * @param addConditionCallback
 * @returns
 */
const addConditionService = (addConditionCallback: (res: any) => void) => {
  const addCondition = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/addCondition`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: addConditionCallback,
    },
  );

  return {
    addConditionRun: addCondition?.run,
    addConditionLoading: addCondition?.loading,
  };
};

/**
 * 编辑流程条件
 * @param editConditionCallback
 * @returns
 */
const editConditionService = (editConditionCallback: (res: any) => void) => {
  const editCondition = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/editCondition`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: editConditionCallback,
    },
  );

  return {
    editConditionRun: editCondition?.run,
    editConditionLoading: editCondition?.loading,
  };
};

/**
 * 模板文件下载
 * @param curRes
 */
const exportEnterService = (callback: (res: any) => void, modalState) => {
  const exportEnter = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        order: 'desc',
        sort: 'createTime',
        type: modalState?.type,
      };
      if (modalState?.applyStorageId) {
        bodyParams.applyStorageId = modalState?.applyStorageId;
      }
      let reqUrl = `${baseUrl}/assetStorage/easyExcelExport`;
      return requestFunHtml('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    exportEnterRun: exportEnter?.run,
    exportEnterLoading: exportEnter?.loading,
  };
};

/**
 * 删除申请
 * @param callback 接口回调
 * @returns
 */
const deleteApplyManageService = (callback: (res: any, clickType: boolean) => void) => {
  const deleteApplyManage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetStorage/cancel`;
      let bodyParams = {
        ...params,
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    deleteApplyManageRun: deleteApplyManage?.run,
    deleteApplyManageLoading: deleteApplyManage?.loading,
  };
};

/**
 * 资产信息转译
 * @param callback 接口回调
 * @returns
 */
const getEnterServiceTranslate = () => {
  const EnterServiceManage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetMaterial/translate`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
    },
  );
  return {
    EnterServiceManageRun: EnterServiceManage?.run,
    EnterServiceManageLoading: EnterServiceManage?.loading,
  };
};
/**
 * 删除物资
 * @param callback 接口回调
 * @returns
 */
const deleteMaterialServices = (callback: (res: any, clickType: boolean) => void) => {
  const deleteMaterial = useRequest(
    (id: string) => {
      let personUrl = `${baseUrl}/assetMaterial/delete/${id}`;
      return requestFunComponFiex('DELETE', personUrl, {});
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    deleteMaterialRun: deleteMaterial?.run,
    deleteMaterialLoading: deleteMaterial?.loading,
  };
};
/**
 * 修改物资
 * @param callback 接口回调
 * @returns
 */
const editMaterialServices = (callback: (res: any, clickType: boolean) => void) => {
  const editMaterial = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetMaterial/edit`;
      let bodyParams = {
        ...params,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    editMaterialRun: editMaterial?.run,
    editMaterialLoading: editMaterial?.loading,
  };
};

/**导出物资清单接口*/
const ExportAssetsServices = () => {
  const request = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/assetStorage/materialListExport`;
      let reqBody = {
        sort: 'id',
        order: 'desc',
        ...payload,
      };
      return requestFunHtml('POST', reqUrl, reqBody);
    },
    {
      manual: true,
    },
  );
  return {
    ExportAssetsRun: request.run,
    ExportAssetsLoading: request.loading,
  };
};

/**
 * 入库登记 - 管理页面 - 导出
 * */
const manageExportEnterService = () => {
  const request = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/assetStorage/export`;
      let reqBody = {
        ...payload,
        state: `${getTenantID()}${ILoadMapTypeAll.ENTER_STORE_STATUS}1003`,
      };
      return requestFunHtml('POST', reqUrl, reqBody);
    },
    {
      manual: true,
    },
  );
  return {
    ExportEnterRun: request.run,
    ExportEnterLoading: request.loading,
  };
};

export {
  deleteApplyManageService,
  addConditionService,
  editConditionService,
  addStorageService,
  editStorageService,
  enrolStorageService,
  getDetailService,
  getStorageTreeService,
  exportEnterService,
  approvedEnterSerivice,
  getEnterServiceTranslate,
  deleteMaterialServices,
  editMaterialServices,
  ExportAssetsServices,
  manageExportEnterService,
};
